package com.yanqu.road.server.manger.maidian;

import com.yanqu.data.analytics.helper.DataBuilder;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.utils.executor.GeneralTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public abstract class BaseEventTraceAction {

    private static Logger logger = LogManager.getLogger(BaseEventTraceAction.class.getName());

    int ret;

    String eventName;

    GamePlayer player;

    UserInfo userInfo;

    DataBuilder dataBuilder;

    public BaseEventTraceAction(String eventName, GamePlayer player) {
        this.eventName = eventName;
        this.player = player;
        this.userInfo = player.getUserInfo();
        dataBuilder = DataBuilder.newBuilder(player.getUserId() + "", DataAnalyticsMgr.getDistinctId(player.getUserInfo()));
    }

    public BaseEventTraceAction(String eventName, UserInfo userInfo) {
        this.eventName = eventName;
        this.userInfo = userInfo;
        dataBuilder = DataBuilder.newBuilder(userInfo.getUserId() + "", DataAnalyticsMgr.getDistinctId(userInfo));
    }

    public BaseEventTraceAction build(String attributeId, Object val){
        if(!DataAnalyticsMgr.openDataAnalytics()){
            return this;
        }
        dataBuilder.getDataMap().put(attributeId, val);
        return this;
    }

    /**
     * 装配参数
     */
    protected BaseEventTraceAction assemble(){
        try {
            if(!DataAnalyticsMgr.openDataAnalytics()){
                return this;
            }
            if(player == null){
                DataAnalyticsMgr.buildPublicEventData(userInfo, dataBuilder);
            }else {
                DataAnalyticsMgr.buildPublicEventData(player, dataBuilder);
            }
            assemble0();

        }catch (Exception e){
            ret = -1;
            logger.error("assemble " + eventName, e);
        }
        return this;
    }

    /**
     * 特殊参数需要装配处理
     */
    protected abstract BaseEventTraceAction assemble0();

    /**
     * 上报
     */
    public void trace(){
        try {
            if(!DataAnalyticsMgr.openDataAnalytics()){
                return;
            }
            DataAnalyticsMgr.addTask(new GeneralTask() {
                @Override
                public void doRun() {
                    try{
                        //改成异步封装、上报
                        assemble();
                        if(ret != 0){
                            return;
                        }
                        DataAnalyticsMgr.provider.track(dataBuilder.getAccountId(), dataBuilder.getDistinctId(), eventName, dataBuilder.getDataMap());
                    }catch (Exception e){
                        logger.error("trace " + eventName, e);
                    }
                }
            });
        }catch (Exception e){
            logger.error("trace " + eventName, e);
        }
    }
}
